/**
 * 版权所有 2009-2012山东新北洋信息技术股份有限公司
 * 保留所有权利。
 */
package com.linyaonan.leetcode.easy._257;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @ProjectName: leetcode
 * @Package: com.linyaonan.leetcode.easy._257
 * @ClassName: BinaryTreePaths
 * @Author: linyaonan
 * @Date: 2019/12/17 15:50
 */
public class BinaryTreePaths {

    public List<String> binaryTreePaths(TreeNode root) {
        List<String> list = new ArrayList<>();
        if (root == null) {
            return list;
        }

        Stack<String> nums = new Stack<>();
        pfNode(root, list, nums);
        return list;
    }

    private void pfNode(TreeNode node, List<String> list, Stack<String> nums) {
        if (node != null) {
            nums.add(String.valueOf(node.val));
            if (node.left != null) {
                pfNode(node.left, list, nums);
                nums.pop();
            }
            if (node.right != null) {
                pfNode(node.right, list, nums);
                nums.pop();
            }
            if (node.right == null && node.left == null) {
                String s = String.join("->", nums);
                list.add(s);
            }
        }
    }

}
